package com.atstudy.headline.web.admin.controller.categories;
import com.atstudy.headline.entity.Categories;
import com.atstudy.headline.result.Result;
import com.atstudy.headline.web.admin.service.CategoriesService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/admin/categories")
public class CategoriesController {


    @Autowired
    private CategoriesService categoriesService;

    /**
     * 内容模块分类管理
     * @return
     */
    @GetMapping("/page")
    public Result<IPage<Categories>> pageDictInfo(@RequestParam Integer current,@RequestParam Integer size) {
        Page<Categories> page = new Page<>(current,size);
        IPage<Categories> categoriesIPage = categoriesService.page(page);
        return Result.ok(categoriesIPage);
    }

    /**
     * 新增/修改
     * @param
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody Categories categories) {
        categoriesService.saveOrUpdate(categories);
        return Result.ok();
    }

    /**
     * 删除
     * @param categoryId
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestParam Integer categoryId) {
        categoriesService.removeById(categoryId);
        return Result.ok();
    }

    /**
     * 修改内容分类状态
     * @return
     */

    @PostMapping("/updateStatus")
    public Result updateStatus(@RequestParam Integer categoryId,@RequestParam Integer status) {
        LambdaUpdateWrapper<Categories> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Categories::getCategoryId,categoryId);
        updateWrapper.set(Categories::getIsActive,status);
        categoriesService.update(updateWrapper);
        return Result.ok();
    }
}
